package ${packageName}

import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.Create${className}Action
import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.Delete${className}Action
import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.List${className}Action
import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.Update${className}Action
import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.Read${className}Action
import ${packageName.replaceAll('.entity','.action')}.${className.toLowerCase()}.Search${className}Action

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON

class ${className}Controller {

    @Autowired
    private Create${className}Action create${className}Action
    @Autowired
    private Update${className}Action update${className}Action
    @Autowired
    private List${className}Action list${className}Action
    @Autowired
    private Delete${className}Action delete${className}Action
    @Autowired
    private Read${className}Action read${className}Action
    @Autowired
    private Search${className}Action search${className}Action

    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
       List list = list${className}Action.execute(params, null)
       render list${className}Action.postCondition(null, list) as JSON
    }

    def show = {
      ${className} ${propertyName} = new ${className}()
      render(template: "show", model:[${propertyName}:${propertyName}])
    }

    def create = {
      ${className} ${propertyName} = new ${className}(params)
      ${className} ${propertyName}Instance = create${className}Action.preCondition(null, ${propertyName})
      Message message = null
      if (${propertyName}Instance == null) {
        message = create${className}Action.getValidationErrorMessageForUI(${propertyName})
      } else {
        ${propertyName}Instance = create${className}Action.execute(null, ${propertyName}Instance)
        if (${propertyName}Instance) {
          message = create${className}Action.getSuccessMessageForUI(${propertyName}Instance, create${className}Action.SUCCESS_SAVE)
        } else {
          message = create${className}Action.getErrorMessageForUI(${propertyName}, create${className}Action.FAIL_SAVE)
        }
      }
      render message as JSON
    }

    def edit = {
      render read${className}Action.execute(params, null) as JSON
    }

    def update = {
      ${className} ${propertyName} = new ${className}(params)
      Object object = update${className}Action.preCondition(params,null)
      Message message = null
      if (object == false) {
          message = update${className}Action.getValidationErrorMessageForUI(${propertyName})
      } else {
          int noOfRows = (int) update${className}Action.execute(null,object)
          if (noOfRows > 0) {
            message = update${className}Action.getSuccessMessageForUI(${propertyName}, update${className}Action.SUCCESS_UPDATE)
          } else {
            message = update${className}Action.getErrorMessageForUI(${propertyName}, update${className}Action.FAIL_UPDATE)
          }
      }
      render message as JSON
    }

    def delete = {
        ${className} ${propertyName} = new ${className}(params)
        ${propertyName}.id = Long.parseLong(params.id)
        ${propertyName}.version = Long.parseLong(params.version)
        Message message = null
        if (${propertyName}) {
          int rowCount = (int) delete${className}Action.execute(null, ${propertyName})
          if (rowCount > 0) {
            message = delete${className}Action.getSuccessMessageForUI(${propertyName}, delete${className}Action.SUCCESS_DELETE)
          } else {
            message = delete${className}Action.getErrorMessageForUI(${propertyName}, delete${className}Action.FAIL_DELETE)
          }
        }
        else {
          message = delete${className}Action.getErrorMessageForUI(${propertyName}, delete${className}Action.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
      ${className} ${propertyName} = search${className}Action.execute(params.fieldName, params.fieldValue)
      if(${propertyName}){
          render ${propertyName} as JSON
      }
      else{
          render ''
      }

    }
}
